/*
 * File:   datetest.c
 * Author: ossi
 *
 * Created on 06-Nov-2011, 17:48:43
 */

#include <stdio.h>
#include <stdlib.h>
#include <CUnit/Basic.h>
#include "date.h"

/*
 * CUnit Test Suite
 */

int init_suite(void) {
    return 0;
}

int clean_suite(void) {
    return 0;
}

void testDate_compare() {
    date date1 = {1,1,2011};
    date date2 = {31,5,2011};
    int result = date_compare(&date1, &date2);
    CU_ASSERT(result < 0);
    
    result = date_compare(&date1, &date1);
    CU_ASSERT(result == 0);
    
    result = date_compare(&date2, &date1);
    CU_ASSERT(result > 0);
}

void testDate_from_string() {
    wchar_t* valid =L"1-1-2011";
    wchar_t* invalid = L"99-99-9999";
    wchar_t* garbage = L"asfd--";
    
    date result = date_from_string(valid);
    date real = {1,1,2011};
    
    CU_ASSERT(date_compare(&real,&result) == 0);
    
    result = date_from_string(invalid);
    CU_ASSERT(date_compare(&NULL_DATE,&result) == 0);
    
    result = date_from_string(garbage);
    CU_ASSERT(date_compare(&NULL_DATE,&result) == 0);
}

void testNew_date() {
    char day = 1;
    char month = 1;
    int year = 2011;
    date real = {1,1,2011};
    date result = new_date(day, month, year);
    CU_ASSERT(date_compare(&real,&result) == 0);
}

int main() {
    CU_pSuite pSuite = NULL;

    /* Initialize the CUnit test registry */
    if (CUE_SUCCESS != CU_initialize_registry())
        return CU_get_error();

    /* Add a suite to the registry */
    pSuite = CU_add_suite("datetest", init_suite, clean_suite);
    if (NULL == pSuite) {
        CU_cleanup_registry();
        return CU_get_error();
    }

    /* Add the tests to the suite */
    if ((NULL == CU_add_test(pSuite, "testDate_compare", testDate_compare)) ||
            (NULL == CU_add_test(pSuite, "testDate_from_string", testDate_from_string)) ||
            (NULL == CU_add_test(pSuite, "testNew_date", testNew_date))) {
        CU_cleanup_registry();
        return CU_get_error();
    }

    /* Run all tests using the CUnit Basic interface */
    printf("datetest.c:");
    CU_basic_set_mode(CU_BRM_VERBOSE);
    CU_basic_run_tests();
    CU_cleanup_registry();
    return CU_get_error();
}
